/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gruppe1projekt.GUILayer;

import gruppe1projekt.ControllerLayer.AuthenticationService;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author Moulde
 */
public class BaseFrame extends JFrame implements ActionListener
    {

    protected BaseWindow currentView;
    protected Dimension windowSize;
    protected JButton btnReturn;
    protected AuthenticationService auth;

    public BaseFrame(String menuTitle)
    {
        auth = AuthenticationService.GetInstance();
        this.windowSize = new Dimension(818, 645);

        setBounds(100, 100, 818, 645);
        setMaximumSize(windowSize);
        setSize(windowSize);
        setPreferredSize(windowSize);

        Initialize(menuTitle);
        this.setTitle(menuTitle);

//        addContentPanel();
        addLogoPanel();

        btnReturn = new JButton();
        btnReturn.setMinimumSize(new Dimension(120, 30));
        btnReturn.setPreferredSize(new Dimension(120, 30));
        btnReturn.setMaximumSize(new Dimension(120, 30));
        btnReturn.setText("Exit");

        btnReturn.addActionListener(new java.awt.event.ActionListener()
            {

            public void actionPerformed(ActionEvent evt)
            {
                ReturnActionPerformed(evt);
            }
            });

        pnlBack.add(btnReturn);

        updateUserStatus();
    }

    public void ReturnActionPerformed(ActionEvent e)
    {
        if (currentView == null)
        {
            return;
        }

        if (currentView.HasInteraction())
        {
            if (JOptionPane.showConfirmDialog(this, "Vil du lukke?") == 0);
            {
                setVisible(false);
            }
        } else
        {
            setVisible(false);
        }
    }

    public void actionPerformed(ActionEvent e)
    {
    }

    public void addButton(JButton button)
    {
        button.setMinimumSize(new Dimension(120, 30));
        button.setPreferredSize(new Dimension(120, 30));
        button.setMaximumSize(new Dimension(120, 30));
        button.setMargin(new Insets(2, 10, 2, 10));
        pnlSidebar.add(button);
        pnlSidebar.add(Box.createRigidArea(new Dimension(0, 5)));
    }

    private void Initialize(String menuTitle)
    {
        pnlBack = new javax.swing.JPanel();
        pnlSidebar = new javax.swing.JPanel();
        pnlContent = new javax.swing.JPanel();
        pnlLogo = new javax.swing.JPanel();

        lblCurrentUser = new javax.swing.JLabel();
        btnLogout = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        pnlBack.setLayout(new FlowLayout());

        pnlSidebar.setBorder(javax.swing.BorderFactory.createTitledBorder(null, menuTitle, javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Verdana", 0, 12))); // NOI18N

        FlowLayout myFlowLayout = new FlowLayout(HEIGHT);

        pnlSidebar.setLayout(myFlowLayout);
        pnlSidebar.setPreferredSize(new Dimension(150, 300));

        pnlContent.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlContent.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        pnlContent.setMaximumSize(new java.awt.Dimension(618, 580));

        pnlLogo.setPreferredSize(new java.awt.Dimension(112, 40));

        javax.swing.GroupLayout pnlLogoLayout = new javax.swing.GroupLayout(pnlLogo);
        pnlLogo.setLayout(pnlLogoLayout);
        pnlLogoLayout.setHorizontalGroup(
                pnlLogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 112, Short.MAX_VALUE));
        pnlLogoLayout.setVerticalGroup(
                pnlLogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 40, Short.MAX_VALUE));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pnlLogo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(pnlBack, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(pnlSidebar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlContent, javax.swing.GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE).addContainerGap()));

        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addComponent(pnlLogo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlSidebar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlBack, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(pnlContent, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 585, Short.MAX_VALUE))
                .addContainerGap()));

        this.add(lblCurrentUser);
        lblCurrentUser.setVisible(true);
        updateUserStatus();
    }

    protected void LogOut(ActionEvent evt)
    {
        auth.Logout();
        updateUserStatus();
    }

    public void updateUserStatus()
    {
        if (auth.IsAuthenticated())
        {
            lblCurrentUser.setText("Bruger: " + auth.User.getFullName());
        } else
        {
            lblCurrentUser.setText("Not Logged in");
        }
    }

    public Dimension getWindowSize()
    {
        return windowSize;
    }

    public void setWindowSize(Dimension windowSize)
    {
        this.windowSize = windowSize;
    }

    private void addLogoPanel()
    {
        ImagePanel logoPanel = new ImagePanel("logo.png");
        insertPanel(pnlLogo, logoPanel);
    }

    private void addContentPanel()
    {
    }

    public BaseWindow getCurrentView()
    {
        return currentView;
    }

    public void setCurrentView(BaseWindow currentView)
    {
        this.currentView = currentView;
    }

    protected void SwitchViewSafe(BaseWindow panel)
    {
        if (currentView != null && currentView.HasInteraction())
        {
            if (currentView.getID() == panel.getID())
            {
                return;
            }

            if (JOptionPane.showConfirmDialog(this, "Der er oplysninger der ikke er gemt, vil du skifte vindue?") == 0)
            {
                SwitchView(panel);
            }
        } else
        {
            SwitchView(panel);
        }
    }

    protected void SwitchView(BaseWindow panel)
    {
        if (panel.RequiresLogin && !auth.IsAuthenticated())
        {
            this.setTitle("Login");

            SwitchView(new LoginView(this, panel));
            updateUserStatus();

            if (!auth.IsAuthenticated())
            {
                return;
            }
        }

        pnlContent.removeAll();
        insertPanel(panel);
        this.setTitle(panel.getName());
        currentView = panel;
    }

    protected void insertPanel(JPanel panel)
    {
        insertPanel(pnlContent, panel);
    }

    protected void insertPanel(JPanel container, JPanel panel)
    {
        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(container);
        container.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout.createSequentialGroup().addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));

        jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel3Layout.createSequentialGroup().addComponent(panel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
    }
    protected javax.swing.JPanel pnlBack;
    protected javax.swing.JPanel pnlContent;
    private javax.swing.JPanel pnlLogo;
    protected javax.swing.JPanel pnlSidebar;
    protected javax.swing.JLabel lblCurrentUser;
    protected javax.swing.JButton btnLogout;
    }
